﻿using SharpEncrypt4.Lib;
using SharpEncrypt4.Lib.Encrypt;
using SharpEncrypt4.Lib.Internal;
using SharpEncrypt4l_Lib;
using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace SharpEncrypt4l.UI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //rsa密钥长度
        private int rsa_key_length_int = 2048;
        public MainWindow()
        {
            InitializeComponent();
        }
        #region 窗体基本控制
        /// <summary>
        /// 鼠标左键按下拖动窗口
        /// </summary>
        /// <param name="sender">事件发送者</param>
        /// <param name="e">路由事件</param>
        private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        /// <summary>
        /// 最小化
        /// </summary>
        /// <param name="sender">消息发送者</param>
        /// <param name="e">路由事件</param>
        private void min_button_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender">消息发送者</param>
        /// <param name="e">路由事件</param>
        private void close_button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 最大化
        /// </summary>
        /// <param name="sender">消息发送者</param>
        /// <param name="e">路由事件</param>
        private void max_button_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;
                normal_button.Visibility = Visibility.Visible;
                max_button.Visibility = Visibility.Collapsed;
            }
            else if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
                normal_button.Visibility = Visibility.Collapsed;
                max_button.Visibility = Visibility.Visible;
            }
        }
        #endregion

        #region AES 加解密算法
        private void aes_encrypt_button_Click(object sender, RoutedEventArgs e)
        {
            string str = aes_encrypt_string.Text;
            if (str.Length > 0)
            {
                //创建AES 秘钥
                AESKey aseKey = EncryptProvider.CreateAesKey();
                string key = aseKey.Key;
                string iv = aseKey.IV;
                ////////////////////AES 加密///////////////////////////////////////
                var srcString = str;
                //不带加密向量
                string encrypted = EncryptProvider.AESEncrypt(srcString, key);
                aes_encrypted_string.Text = encrypted;
                srcString = str;
                //带加密向量
                string ivEncrypted = EncryptProvider.AESEncrypt(srcString, key, iv);
                aes_iv_encrypted_string.Text = ivEncrypted;
                ///////////////////AES 解密///////////////////////////////////////
                string encryptedStr = encrypted;
                //不带加密向量（与加密方式对应）
                string decrypted = EncryptProvider.AESDecrypt(encryptedStr, key);
                aes_decrypted_string.Text = decrypted;
                encryptedStr = ivEncrypted;
                //带加密向量
                string ivDecrypted = EncryptProvider.AESDecrypt(encryptedStr, key, iv);
                aes_iv_decrypted_string.Text = ivDecrypted;
            }
        }

        #endregion

        #region DES加解密处理
        private void des_encrypt_button_Click(object sender, RoutedEventArgs e)
        {
            string str = des_encrypt_string.Text;
            if (str.Length > 0)
            {
                //创建DES 秘钥
                //秘钥长度为24
                var desKey = EncryptProvider.CreateDesKey();
                ///////////////////////////DES加密///////////////////////////
                var srcString = str;
                var encrypted = EncryptProvider.DESEncrypt(srcString, desKey);
                des_encrypted_string.Text = encrypted;
                //DES解密
                var encryptedStr = encrypted;
                var decrypted = EncryptProvider.DESDecrypt(encryptedStr, desKey);
                des_decrypted_string.Text = decrypted;
            }

        }
        #endregion

        #region RSA加解密处理
        private void rsa_encrypt_button_Click(object sender, RoutedEventArgs e)
        {
            string str = rsa_encrypt_string.Text;
            string privateKey = rsa_private_key_string.Text;
            string publicKey = rsa_public_key_string.Text;
            if (str.Length > 0 && privateKey.Length > 0 && publicKey.Length > 0)
            {
                publicKey = RSAKeyConvert.RSAPublicKeyJava2DotNet(publicKey);
                //RSA加密
                string srcString = str;
                string encrypted = EncryptProvider.RSAEncrypt(publicKey, srcString);
                rsa_encrypted_string.Text = encrypted;
                privateKey = RSAKeyConvert.RSAPrivateKeyJava2DotNet(privateKey);
                //RSA解密
                string encryptedStr = encrypted;
                string decrypted = EncryptProvider.RSADecrypt(privateKey, encryptedStr);
                rsa_decrypted_string.Text = decrypted;
            }
        }
        private void rsa_create_button_Click(object sender, RoutedEventArgs e)
        {
            RsaSize rsaSize = RsaSize.R2048;
            switch (rsa_key_length_int)
            {
                case 2048:
                    rsaSize = RsaSize.R2048;
                    break;
                case 3072:
                    rsaSize = RsaSize.R3072;
                    break;
                case 4096:
                    rsaSize = RsaSize.R4096;
                    break;
                case 5120:
                    rsaSize = RsaSize.R5120;
                    break;
            }
            //创建RSA秘钥
            RSAKey rsaKey = EncryptProvider.CreateRsaXmlKey(rsaSize);    //default is 2048
            MessageBox.Show("公钥私钥已经生成，抢到" + AppDomain.CurrentDomain.BaseDirectory + "\\Converter文件夹下查找");
        }
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton btn = sender as RadioButton;
            if (btn == null)
                return;
            if (btn.Name == "ras_key_length_2048")
            {
                rsa_key_length_int = 2048;
            }
            else if (btn.Name == "ras_key_lengt_3072")
            {
                rsa_key_length_int = 3072;
            }
            else if (btn.Name == "ras_key_length_4096")
            {
                rsa_key_length_int = 4096;
            }
            else if (btn.Name == "ras_key_length_5120")
            {
                rsa_key_length_int = 5120;
            }
            else
            {
                rsa_key_length_int = 2048;
            }
        }
        #endregion

        #region HASH操作
        private void hash_encrypt_button_Click(object sender, RoutedEventArgs e)
        {
            string key = hash_key_string.Text;
            string srcString = hash_encrypt_string.Text;
            if (key.Length > 0 && srcString.Length > 0)
            {
                //////////////////////////////////MD5//////////////////////////////
                hash_md5_string.Text = EncryptProvider.Md5(srcString);
                ///////////////////////////////////SHA////////////////////////////
                //SHA1
                hash_sha1_string.Text = EncryptProvider.Sha1(srcString);
                //SHA256
                hash_sha256_string.Text = EncryptProvider.Sha256(srcString);
                //SHA384
                hash_sha384_string.Text = EncryptProvider.Sha384(srcString);
                //SHA512
                hash_sha512_string.Text = EncryptProvider.Sha512(srcString);
                //////////////////////////////////HMAC/////////////////////////////
                //MD5
                hash_hmacmd5_string.Text = EncryptProvider.HMACMD5(srcString, key);
                //HMACSHA1
                hash_hmacsha1_string.Text = EncryptProvider.HMACSHA1(srcString, key);
                //HMACSHA256
                hash_hmacsha256_string.Text = EncryptProvider.HMACSHA256(srcString, key);
                //HMACSHA384
                hash_hmacsha384_string.Text = EncryptProvider.HMACSHA384(srcString, key);
                //HMACSHA512
                hash_hmacsha512_string.Text = EncryptProvider.HMACSHA512(srcString, key);
            }
        }
        #endregion

        #region base64操作
        private void base64_encrypt_button_Click(object sender, RoutedEventArgs e)
        {
            string srcString = base64_encrypt_string.Text;
            if (srcString.Length > 0)
            {
                //Base64加密
                base64_encrypted_utf8_string.Text = EncryptProvider.Base64Encrypt(srcString);   //default encoding is UTF-8
                base64_encrypted_ascii_string.Text = EncryptProvider.Base64Encrypt(srcString, Encoding.ASCII);
                //Base64解密
                base64_decrypted_utf8_string.Text = EncryptProvider.Base64Decrypt(base64_encrypted_utf8_string.Text);   //default encoding is UTF-8
                //Encoding.ASCII(需要与加密编码对应)
                base64_decrypted_ascii_string.Text = EncryptProvider.Base64Decrypt(base64_encrypted_ascii_string.Text, Encoding.ASCII);
            }
        }
        #endregion
    }
}
